//
//  MainViewController.swift
//  sinaweiboFirst
//
//  Created by mac on 16/3/6.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
    
    
    @objc private func plusBtnDidClick() {
        print(__FUNCTION__)
        let compose = ComposeViewController()
        let nav = UINavigationController(rootViewController: compose)
        //modal出 发布微博的控制器
        presentViewController(nav, animated: true, completion: nil)
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        let mainTabBar = MainPlusTabBar()
        //给自定义的按钮添加点击事件
        
        mainTabBar.plusBtn.addTarget(self, action: "plusBtnDidClick", forControlEvents: .TouchUpInside)
        
        
        //kvc给对象提供类似字典的方式 访问对象的属性
        // 系统的tabbar只读 不能直接操作
        //系统的tabbar
        //        print(tabBar.classForCoder)  UITabBar
        //添加自定义的tabbar
        setValue(mainTabBar, forKey: "tabBar")
        //添加后的tabbar
        //        print(tabBar.classForCoder)  MainPlusTabBar
        
        //添加子控制器
        addChildViewControllers()
        
        // 如果把自定义的button放这里 字体颜色会是系统颜色 -> 蓝色
        //        let mainTabBar = MainPlusTabBar()
    }
    
    
    func addChildViewControllers(){
        addChildViewController(HomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(DiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(ProfileTableViewController(), title: "我", imageName: "tabbar_profile")
    }
    
    func addChildViewController(VC: UIViewController,title: String,imageName: String) {
        
        //文字
        VC.title = title
        VC.tabBarItem.title = title
        //颜色 Common的全局颜色
//        tabBar.tintColor = UIColor.orangeColor()
        
        //        //设置字体
        //        VC.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: .Selected)
        
        //图片
        VC.tabBarItem.image = UIImage(named: imageName)
        
        //选中图片 可以省略枚举名
        VC.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        
        //根控制器
        let rootVC = UINavigationController(rootViewController: VC)
        addChildViewController(rootVC)

    }
}
